;(function($){
	$.fn.spa_discountZero = function(opts){
		var el = this;
		var opt = {
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		var t = this;
		this.initHtml();
		this.getDiscount(function(data){
			t.initDiscountHtml(data);
			t.bindEvent();
		})
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//关闭
			el.find("#close-dialog,#modalBtnCancel").unbind("click").click(function(){
				el.modal("hide");
				el.empty();
				param.closeBack();
			});
			
			//输入应收金额
			el.find("#receivable_help").unbind("click").click(function(){
				//计算器
				var status = false;
				el.find("#modal-dialog").hide();
				if(!$.isFunction($().wechatAlipayPay)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/openSpa/jquery.plugin.digitalCalculator.js");
		            $.ajaxSetup({ async: true });
				}
				el.find("#mykeyboard").digitalCalculator({
					number:el.find("#receivable").val()?el.find("#receivable").val():1,//数量
					status:status?status:false,
					title:param.title?param.title:"",
					//初始化数据返回回调
					dataBack:function(number){
						el.find("#modal-dialog").show();
						el.find("#mykeyboard").hide();
						el.find("#receivable").val(number);
						
						var totalprice = (parseFloat(param.togatherPrice)||0.00) + parseFloat(param.balance)
						var disrate = parseFloat(totalprice) - parseFloat(number)
						disrate = checkTwoPoint(disrate)
						el.find("#disrate").val(disrate);
					},
					returnBack :function(){
						el.find("#modal-dialog").show();
						el.find("#mykeyboard").hide();
					}
				});
			});
			
			//输入抹零金额
			el.find("#disrate_help").unbind("click").click(function(){
				//计算器
				var status = false;
				el.find("#modal-dialog").hide();
				if(!$.isFunction($().wechatAlipayPay)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/openSpa/jquery.plugin.digitalCalculator.js");
		            $.ajaxSetup({ async: true });
				}
				el.find("#mykeyboard").digitalCalculator({
					number:el.find("#disrate").val()?el.find("#disrate").val():1,//数量
					status:status?status:false,
					title:param.title?param.title:"",
					//初始化数据返回回调
					dataBack:function(number){
						el.find("#modal-dialog").show();
						el.find("#mykeyboard").hide();
						el.find("#disrate").val(number);
						var totalprice = (parseFloat(param.togatherPrice)||0.00) + parseFloat(param.balance)
						var disrate = parseFloat(totalprice) - parseFloat(number)
						disrate = checkTwoPoint(disrate)
						el.find("#receivable").val(disrate);
					},
					returnBack :function(){
						el.find("#modal-dialog").show();
						el.find("#mykeyboard").hide();
					}
				});
			});
			
			
    		//确认
    		el.find("#modalBtnSave").unbind("click").click(function(){
    			var postData={};
    			postData.ct_brzd = {"no":param.no,"zkl":el.find("#coupon_rate input").val(),"str4":""};
    			t.saveOper(postData);
    		})
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="mykb-box" id="mykeyboard"></div>'+
						'<div id="modal-dialog" class="modal-dialog" style="width:600px;">'+
							'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="title-dialog">'+$.i18n.propJsp("折扣抹零")+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body" style="min-height: 100px;">'+
									'<form id="frm" class="form-horizontal">'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("应收:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group" id="coupon_receivable" style="width:360px;">'+
													'<input type="text" class="form-control search-query input_all" placeholder="" id="receivable" name="receivable">'+
													'<span class="input-group-btn">'+
														'<button type="button" class="btn btn-primary btn-sm" id="receivable_help">'+
															'<span class="ace-icon fa fa-keyboard icon-on-right bigger-110"></span>'+
															$.i18n.propJsp("选择")+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
										'<div class="space-6"></div>'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("抹零:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group" id="coupon_rate" style="width:360px;">'+
													'<input type="text" class="form-control search-query input_all" placeholder="" id="disrate" name="disrate">'+
													'<span class="input-group-btn">'+
														'<button type="button" class="btn btn-primary btn-sm" id="disrate_help">'+
															'<span class="ace-icon fa fa-keyboard icon-on-right bigger-110"></span>'+
															$.i18n.propJsp("选择")+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
									'</form>'+
									'<div class="widget-box transparent modal-list">'+  
										'<div class="orderTaste"></div>'+
									'</div>'+	
								'</div>'+
								'<div class="modal-footer">'+
									'<div class="clearfix">'+
										'<div class="pull-right">'+
											'<div class="btn-group">'+
												'<button id="modalBtnSave" type="button" class="btn btn-sm btn-info">'+
													'<i class="ace-icon fa fa-check"></i>'+
													$.i18n.propJsp("确认")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnCancel" type="button" class="btn btn-sm btn-default">'+
													'<i class="ace-icon fa fa-times"></i>'+
													$.i18n.propJsp("返回")+
												'</button>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			el.html(html);
		},
		getDiscount : function(back){
			var el = this.el;
			var param = this.param;
			var t = this;
			t.wait();
			var arg = {arg:{no:param.no}}
			jQuery.ajax({  
	    		type : 'POST',
	    		url : $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
	    		data : $.HSAPI_ASKJSONINFO( JSON.stringify(arg),'POSDiscountDefault',"{}",""),
	    		dataType : 'json',  
	    		success : function(data){
	    			t.goon();
	    			if(data.RETURN&&data.RETURN!=1){
	    				jf_alert(data.MSG);
	    			}else{
	    				back(data);
	    			}
	    		},  
	    		error : function(data){
	    			t.goon();
	    			jf_alert(JSON.stringify(data));
	    		}  
	    	});
		},
		initDiscountHtml : function(data){
			var el = this.el;
			var param = this.param;
			var t = this;
			
    		if(data.ct_brzd.length > 0){
        		if(!!data.ct_brzd[0].zkl){
        			el.find("#coupon_rate input").val(data.ct_brzd[0].zkl);
        		}
    		}
			el.find("#coupon_rate button").attr("data-json",JSON.stringify(data.discoderate));
		},
		saveOper : function(postData){
    		var t=this;
    		var el=this.el;
    		var param=this.param;
    		t.wait();
    		$.ajax({
    			url : $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
    			type : "post",
    			dataType : "json",
    			data : $.HSAPI_ASKJSONINFO(JSON.stringify(postData),'POSCT_brzdEditSave',"{}"),
    			success : function(data){
    				t.goon();
					if(data.RETURN != -1){
    					param.dataBack();
    					el.modal("hide");
    					el.empty();
    				}else{
    					jf_alert(data.MSG);
    				}
    			},
    			error : function(data){
    				t.goon();
        			jf_alert(JSON.stringify(data));
    			},
    		});
    	},
		wait : function(){
			var el = this.el;
			el.find("#loading").removeClass("hide");
			el.find("#pageOverlay").removeClass("hide");
		},
		goon : function(){
			var el = this.el;
			el.find("#loading").addClass("hide");
			el.find("#pageOverlay").addClass("hide");
		},
	}
	
	function checkTwoPoint(value){  		
	    value=value.toString()+".00";
	    value = value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
	    value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
	    value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
	    return value;
	}
})(jQuery);